<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .pic1 {
            position: absolute;
            width: 400px;
            height: 300px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="pic1"></div>
    <script>
        // 核心算法:移动的距离=目标值-现在位置/10
        function animate(obj, target) {
            obj.timer = setInterval(function() {
                var move = (target - obj.offsetLeft) / 10;
                move > 0 ? Math.ceil(move) : Math.floor(move);
                if (obj.offsetLeft >= target) {
                    clearInterval(obj.timer);
                } else {

                    obj.style.left = obj.offsetLeft + move + 'px';
                }
            }, 15)

        }
        var pic1 = document.querySelector('.pic1');
        animate(pic1, 900)
    </script>
</body>

</html>